<?php
class Keyword_AdminController extends Ac_AdminController
{
	
	public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }
    
	public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction()
    {
    	if(is_object(Model_Config::one()))
    	{	
    		$obj = Model_Config::one();
    		
    		$per_page = 10;
        	$page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        	$this->view->per_page = $per_page;
        	$this->view->listing_number = count($obj->keyword);
        	$this->view->page = $page;
  			$skip_page = 0;
	    	if ($page>1) {
	            $skip_page = ($_GET['page']-1) * $per_page;
	        }
    		//$this->view->keywords = $obj->keyword;
    		$arr = $obj->keyword;
    		$this->view->keywords = array_slice($arr, $skip_page, $per_page);
    	}
    	$this->render('list');
    }
    
    public function addAction()
    {
    	if($this->_request->isPost())
    	{
    		$post = $this->_request->getPost();
    		$keywords = $post['keywords'];
    		$arr = explode("\r\n", $keywords);
    		$arr = array_unique($arr);
    		$arr_une = array();
    		foreach ($arr as $val)
    		{
    			if($val == '')
    			{
    				continue;
    			}
    			$arr_une[] = $val;
    		}
    		if(is_object(Model_Config::one()))
    		{	
    			$obj = Model_Config::one();
    			foreach ($arr_une as $v)
    			{
    				if(!in_array($v, $obj->keyword))
    					Model_Config::update(array('_id'=>new MongoID($obj->_id)), array('$push'=>array('kw'=>$v)));
    			}
    		}
    		else{
    			$obj = new Model_Config();
    			$obj -> keyword = $arr_une;
    			$obj -> save();
    		}
    		$this->_redirect($this->view->geturl('list','keyword', null, 'admin1'));
    	}
    	$this->render('add');	
    		/*
    		$keyword = $_FILES['keywords'];
    		$ext = pathinfo($keyword['name']);
    		if($ext['extension'] != 'txt')
    		{
    			throw new Exception('Your upload file is not .txt, please check again');
    		}
    		$c = file($keyword['tmp_name']);
    		$c = array_unique($c);
    		if(is_object(Model_Config::one()))
    		{	
    			$obj = Model_Config::one();
    			foreach ($c as $val)
    			{
    				$val = str_replace("\r\n", "", $val);
    				if($val == "")
    				{
    					continue;
    				}
    				$val = mb_convert_encoding($val, 'UTF-8','GB2312,UTF-8,GBK,Unicode');
    				if(!in_array($val, $obj->keyword)){
    					Model_Config::update(array('_id'=>new MongoID($obj->_id)), array('$push'=>array('kw'=>$val)));
    				}
    			}
    		}
    		else{
    			$obj = new Model_Config();
    			foreach ($c as $k=>$v)
    			{
    				$v = str_replace("\r\n", "", $v);
    				if($v == "")
    				{
    					continue;
    				}
    				$c_e[] = mb_convert_encoding($v, 'UTF-8','GB2312,UTF-8,GBK,Unicode');
    			}
    			$obj -> keyword = $c_e;
    			$obj -> save();
    		} 
    		$this->_redirect($this->view->geturl('list','keyword', null, 'admin1'));   		
    	}
    	$this->render('add');
    	*/
    }
    
    public function removeAction()
    {
    	$id = $_GET['id'];
		$obj = Model_Config::one();
		Model_Config::update(array('_id'=>new MongoID($obj->_id)), array('$pull'=>array('kw'=>$id)));
    	$this->_redirect($this->view->geturl('list','keyword', null, 'admin1'));
    }
    
    public function editAction()
    {
    	$id = $_GET['id'];
    	$obj = Model_Config::one();
    	if($this->_request->isPost())
    	{
	    	$newkeyword = $_POST['newkeyword'];
	    	Model_Config::update(array('_id'=>new MongoID($obj->_id)), array('$set'=>array('kw.'.$id=>$newkeyword)));
	    	$this->_redirect($this->view->geturl('list','keyword', null, 'admin1'));
    	}
    	$this->view->keyword =  $obj->keyword[$id];;
    	$this->render('edit');
    }
    
    public function testAction()
    {
	   	$listingScore = new Model_ListingScore();
    	$likeList = $listingScore->getLikeNum();			//dump($likeList, 'likeList');
    	$visitList = $listingScore->getVisitNum();			//dump($visitList, 'visitList');
    	$listOfLikeVist = array_merge_recursive($likeList, $visitList);		//dump($listOfLikeVist, 'listOfLikeVist');
    	$filterUser = $listingScore->filterUser($listOfLikeVist); //dump($filterUser);
    	$getTopScoreList = $listingScore->getTopScoreList($filterUser); //dump($getTopScoreList);
    	$this->view->list = $getRelatedList = $listingScore->getRelatedList($getTopScoreList);
    	
    	
    	$this->render('test');
    }
    
    public function cronjobAction()
    {
//		$listing = Model_Listing::one();
//		echo $listing->title_sc;
//		$user = Model_User::one(array('em'=>'ck@orientationsys.com'));
//		Ac_Util::sendEmail('test', 'test', 'ck@orientationsys.com', true, array('user' => $user, 'listing' => $listing), Ac_Mail::TEMPLATE_SIMILAR_LIST);

    	$listingScore = new Model_ListingScore();
    	$likeList = $listingScore->getLikeNum();
    	$visitList = $listingScore->getVisitNum();
    	$listOfLikeVist = array_merge_recursive($likeList, $visitList);
    	$filterUser = $listingScore->filterUser($listOfLikeVist);
    	$getTopScoreList = $listingScore->getTopScoreList($filterUser);
    	$getRelatedList = $listingScore->getRelatedList($getTopScoreList);
    	foreach ($getRelatedList as $user=>$listings)
    	{
    		Ac_Util::sendEmail('您也许喜欢的宝贝', 'test', Model_User::find(new MongoId($user))->em, true, array('user' => Model_User::find(new MongoId($user)), 'listings' => $listings), Ac_Mail::TEMPLATE_SIMILAR_LIST);
    		Model_User::update(array('_id'=>new MongoId($user)), array('$set'=>array('emd'=>'1')));
    	}
    	print 'Emial successfully';
    	die();
    }
    
}